package baseproject.com.mybaseproject.mvp.presenter

import baseproject.com.mybaseproject.mvp.contract.MainContract
import baseproject.com.mybaseproject.net.ApiErrorModel
import baseproject.com.mybaseproject.net.ResultObserver
import baseproject.com.mybaseproject.net.RetrofitManager
import baseproject.com.mybaseproject.utils.scheduler.RxUtils
import com.bybops.app.MyApplication
import com.bybops.model.bean.EquipmentList
import com.bybops.net.BaseResponse

class MainPresenterImpl : BasePresenterImpl<MainContract.MainView>() , MainContract.MainPresenter{


    var pageIndex = 0
    var arrTemp: ArrayList<EquipmentList>? = ArrayList()
    /**
     * 获取工作列表
     */
    override fun appEquipmentGetList(refresh: Boolean) {
        var params = HashMap<String, String?>()
        if (refresh) pageIndex = 0 else pageIndex++
        params["access_token"] = MyApplication.instance?.mAccessToken
        params["hospital"] = ""
        params["equipStatus"] = ""
        params["pageSize"] = "20"
        params["pageIndex"] = pageIndex.toString()
        addDisposable(RetrofitManager.apiService.appEquipmentGetList(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<ArrayList<EquipmentList>>() {
                    override fun success(data: BaseResponse<ArrayList<EquipmentList>>) {
                        if (pageIndex == 0) {
                            //说明是首页
                            arrTemp?.clear()
                        }
                        arrTemp?.addAll(data.data)
                        mView?.appEquipmentGetListSucess(arrTemp ?: ArrayList())
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        pageIndex--
                        mView?.appEquipmentGetListFail(apiErrorModel.message)
                    }
                }))
    }

    override fun loginOut() {
        var params = HashMap<String , String?>()

        params["access_token"] = MyApplication.instance?.mAccessToken

        mView?.showProgress("正在加载...")
        addDisposable( RetrofitManager.apiService.loginOut(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<String>(){
                    override fun success(data: BaseResponse<String>) {
                        mView?.hideProgress()
                        mView?.loginOutSucess(data.resultMsg)
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        mView?.hideProgress()
                        mView?.loginOutFailed(apiErrorModel.message)
                    }
                }))
    }

    /**
     * 根据equipSN获取设备详情
     */
    override fun equipmentGetDetail(equipSN: String?) {
       var params = HashMap<String , String?>()
        params["access_token"] = MyApplication.instance?.mAccessToken
        params["equipSN"] = equipSN
        mView?.showProgress("")
        addDisposable(RetrofitManager.apiService.equipmentGetDetail(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<EquipmentList>(){
                    override fun success(data: BaseResponse<EquipmentList>) {
                        mView?.hideProgress()
                        mView?.equipmentGetDetailSuccess(data.data)
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        mView?.hideProgress()
                        mView?.showToast(apiErrorModel.message)
                    }
                }))
    }
}